//
//  XCSaveController.m
//  TTLestening
//
//  Created by dllo on 16/4/10.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCSaveController.h"
#import <AVOSCloud.h>
#import "XCModelForUser.h"
#import "HYTableViewCell2.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface XCSaveController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSMutableArray *array;

@end

@implementation XCSaveController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubview];
    
    [self pullLead];
    
}

- (void)pullLead {
    
    AVUser *user = [AVUser currentUser];
    AVQuery *query = [AVQuery queryWithClassName:[NSString stringWithFormat:@"c%@", user.username]];
    self.array = [NSMutableArray array];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
      //  NSLog(@"%ld", objects.count);
        
      //  NSLog(@"%@", objects);
        for (AVObject *object in objects) {
            XCModelForUser *model = [[XCModelForUser alloc] init];
            model.songName = [object objectForKey:@"songName"];
            model.pic = [object objectForKey:@"pic"];
            
            [self.array addObject:model];
            
            
        }
        
        [self.tableview reloadData];
    }];
    
}


- (void)createSubview {
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"HYTableViewCell2" bundle:[NSBundle mainBundle]]forCellReuseIdentifier:@"poolForUser"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HYTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForUser"];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    XCModelForUser *mdoel = [self.array objectAtIndex:indexPath.row];
    cell.title.text = mdoel.songName;
    cell.songercount.text = @"";
    [cell.imageback sd_setImageWithURL:[NSURL URLWithString:mdoel.pic]];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.frame.size.height / 10;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
